# --*-- coding:utf-8 --*--

import warnings
warnings.filterwarnings("ignore")
import requests
import datetime
import time
import math
from multiprocessing import Pool
from multiprocessing.dummy import Pool
import random


class prodouct_keyword_data():

	all_data = {} #装图跟表数据的容器

	cookies = None

	head = {
			'Host': 'sycm.taobao.com',
			'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'

	}

	dateRange = None

	itemId = None


	def dateRanges(self,beginDate, endDate):
	    dates = []
	    dt = datetime.datetime.strptime(beginDate, '%Y-%m-%d')
	    date = beginDate[:]
	    while date <= endDate:
	        dates.append(date)
	        dt = dt + datetime.timedelta(1)
	        date = dt.strftime('%Y-%m-%d')
	    return dates

	def UnitPrice(self,goodsid,date):

		''' 获取商品客单价 '''

		url = 'https://sycm.taobao.com/cc/cockpit/marcro/item/top.json'

		all_day_data = []


		time.sleep(random.uniform(1,3))

		dateRange = str(date) +"|"+ str(date)

		payload = {
			"dateRange":dateRange,
			"dateType":"day",
			"pageSize":"10",
			"page":"1",
			"order":"desc",
			"orderBy":"payAmt",
			"keyword":goodsid,
			"follow":"false",
			"cateId":"",
			"cateLevel":"",
			"guideCateId":"",
			"device":"0",
			"indexCode":"itmUv,itemCartCnt,payItmCnt,payAmt,payRate",
			"_":"1540976730914",
			"token":"4a17c83e2"

		}
		resp = requests.get(url,headers = self.head,cookies = self.cookies,verify=False,params = payload)
		data = resp.json()['data']['data'][0]
		if data['payByrCnt']['value'] == 0:
			unitprice = 0
		else:
			unitprice = data['payAmt']['value'] / data['payByrCnt']['value']
		return unitprice

	# def mon_keyword_data(self):

	# 	#单品关键词数据分析30天数据

	# 	dateRange = str(self.dateRange['start_time']) + '|' + str(self.dateRange['end_time'])

	# 	all_keyword_data = []

	# 	# id_list = ['561985924300']

	# 	# for itemId in id_list:
	# 	#取出关键词数据
	# 	url = 'https://sycm.taobao.com/flow/v3/new/item/source/detail.json'
	# 	data = {
	# 			'dateRange':dateRange,
	# 			'dateType':'recent30',
	# 			'pageSize'	:'10',
	# 			'page':'1',
	# 			'order':'desc',
	# 			'orderBy':'uv',
	# 			'itemId':self.itemId,
	# 			'device':'2',
	# 			'pageId':'23.s1150',
	# 			'pPageId':'23',
	# 			'pageLevel':'2',
	# 			'childPageType':'se_keyword',
	# 			'belong':'all',
	# 			'indexCode':'uv,crtByrCnt,crtRate',
	# 			'_':'1536734494730',
	# 			'token':'1a998269c'
	# 	}
	# 	# 无线
	# 	wx_resp = requests.get(url,headers = self.head,cookies = self.cookies,verify=False,params = data)
	# 	# print (wx_resp.json())
	# 	page_con = math.ceil(wx_resp.json()['data']['recordCount']/10)
	# 	for i in range(1,page_con + 1):
	# 		time.sleep(1)
	# 		data['page'] = str(i)
	# 		resp = requests.get(url,headers = self.head,cookies = self.cookies,verify=False,params = data)
	# 		for k in resp.json()['data']['data']:
	# 			all_keyword_data.append(
	# 					{
	# 					 "uv":k['uv']['value'],  #访客数
	# 					 "cltCnt":k['cltCnt']['value'],  #收藏人数
	# 					 "cartByrCnt":k['cartByrCnt']['value'],   #加购数
	# 					 "payByrCnt":k['payByrCnt']['value'],  #支付买家数
	# 					 "payRate":k['payRate']['value'],    # 支付转化率
	# 					 "keyword":k['pageName']['value'],
	# 					 "statDate":k['statDate']['value']
	# 			 })

	# 	self.all_data['table'] = all_keyword_data  #装入数据


	def day_keyword_data(self,date):

		# 单品关键词每天数据

		#计算当天商品客单价
		try:
			unitprice = self.UnitPrice(self.itemId,date)
		except:
			return {'msg': 'You must login system first.', 'code': 5810}


		day_all_data = []

		time.sleep(random.uniform(1,5))

		date = str(date) + "|" + str(date)

		keyword_data = []

		# id_list = ['561985924300']

		# for itemId in id_list:
		#取出关键词数据
		url = 'https://sycm.taobao.com/flow/v3/new/item/source/detail.json'
		data = {
				'dateRange':date,
				'dateType':'day',
				'pageSize'	:'10',
				'page':'1',
				'order':'desc',
				'orderBy':'uv',
				'itemId':self.itemId,
				'device':'2',
				'pageId':'23.s1150',
				'pPageId':'23',
				'pageLevel':'2',
				'childPageType':'se_keyword',
				'belong':'all',
				'indexCode':'uv,crtByrCnt,crtRate',
				'_':'1536734494730',
				'token':'1a998269c'
		}
		# 无线
		wx_resp = requests.get(url,headers = self.head,cookies = self.cookies,verify=False,params = data)

		# print (wx_resp.text)

		try:
			page_con = math.ceil(wx_resp.json()['data']['recordCount']/10)
			for i in range(1,page_con + 1):
				time.sleep(random.uniform(0.5,2))
				data['page'] = str(i)
				resp = requests.get(url,headers = self.head,cookies = self.cookies,verify=False,params = data)
				for k in resp.json()['data']['data']:

					# 取数整理
					keyword = k['pageName']['value']
					try:
						uv = k['uv']['value']  #访客数
					except:
						uv = ''
					try:	
						jpSelfUv = k['jpSelfUv']['value'] #店内跳转人数
					except:
						jpSelfUv = ''
					try:
						jpUv = k['jpUv']['value'] # 店铺跳出人数
					except:
						jpUv = ''
					try:						
						cltCnt = k['cltCnt']['value']  #收藏人数
					except:
						cltCnt = ''
					try:
						cartByrCnt = k['cartByrCnt']['value']   #加购数
					except:
						cartByrCnt = ''
					try:
						payByrCnt = k['payByrCnt']['value']  #支付买家数
					except:
						payByrCnt = ''
					try:
						payRate = k['payRate']['value']    # 支付转化率
					except:
						payRate = ''
					try:
						statDate = k['statDate']['value']
					except:
						statDate = ''
					try:
						fansPayByrCnt = k['fansPayByrCnt']['value'] #粉丝买家数
					except:
						fansPayByrCnt = ''
					try:
						cltItmPayByrCnt = k['cltItmPayByrCnt']['value']  #收藏商品-支付买家数
					except:
						cltItmPayByrCnt = ''
					try:	
						ordItmPayByrCnt = k['ordItmPayByrCnt']['value']  #加购商品-支付买家数
					except:
						ordItmPayByrCnt = ''

					day_all_data.append(
							{
							"keyword": keyword,
							 "uv": uv,
							 "jpSelfUv": jpSelfUv,
							 "jpUv": jpUv,
							 "cltCnt": cltCnt,					 
							 "cartByrCnt": cartByrCnt,
							 "payByrCnt": payByrCnt,  
							 "payRate": payRate,
							 "statDate": statDate,
							 "fansPayByrCnt": fansPayByrCnt,
							 "cltItmPayByrCnt": cltItmPayByrCnt,
							 "ordItmPayByrCnt": ordItmPayByrCnt,
							 "unitprice" : unitprice
					 })
			return day_all_data
		except:
			return wx_resp.json()


	# def get_day_data(self):

	# 	# 最近30天数据
	# 	self.mon_keyword_data()

	# 	# 每天数据
	# 	time_list = self.dateRanges(self.dateRange['start_time'],self.dateRange['end_time'])
	# 	pool = Pool(10)
	# 	data = pool.map(self.day_keyword_data,time_list)
	# 	pool.close()
	# 	pool.join()

	# 	#数据处理
	# 	mon_data = []
	# 	map_key = []
	# 	for i in data:
	# 		for k in i:
	# 			map_key.append(k['keyword'])
			
	# 	Duplicate_key_words = list(set(map_key))

	# 	# 30天数据处理
	# 	for day in data:

	# 		key_list = []
	# 		day_data = []
	# 		for h in Duplicate_key_words:
	# 			for j in day:
	# 				key_list.append(j['keyword'])
	# 				statDate = j['statDate']

	# 			if h in key_list:
	# 				for j in day:
	# 					if h == j['keyword']:
	# 						day_data.append(j)
	# 			else:
	# 				day_data.append({
	# 							'cltCnt': 0,
	# 							'keyword': h,
	# 							'cartByrCnt': 0,
	# 							'payByrCnt': 0,
	# 							'uv': 0,
	# 							'payRate': 0,
	# 							'statDate':statDate
	# 						})

	# 		mon_data.append(day_data)
		
	# 	self.all_data['map'] = mon_data
	# 	return self.all_data

	def Any_day_data(self,dates):

		# 任意N天数据

		time_list = self.dateRanges(dates['start_time'],dates['end_time'])
		pool = Pool(math.ceil(len(time_list)/5))
		data = pool.map(self.day_keyword_data,time_list)
		pool.close()
		pool.join()
		
		try:
			data[0]['code']
			return data[0]
		except:

			#数据处理
			mon_data = []
			map_key = []
			for i in data:
				for k in i:
					map_key.append(k['keyword'])
				
			Duplicate_key_words = list(set(map_key))

			# 30天数据处理
			for day in data:

				key_list = []
				day_data = []
				for h in Duplicate_key_words:
					for j in day:
						key_list.append(j['keyword'])
						statDate = j['statDate']

					if h in key_list:
						for j in day:
							if h == j['keyword']:
								day_data.append(j)
					else:
						day_data.append({
									'cltCnt': 0,
									'keyword': h,
									'cartByrCnt': 0,
									'payByrCnt': 0,
									'uv': 0,
									'payRate': 0,
									'statDate':statDate
								})

				mon_data.append(day_data)
			
			return mon_data




if __name__ == '__main__':
	
    prodouct_keyword_data = prodouct_keyword_data()
    prodouct_keyword_data.itemId = '569541384926'  # 需要转入一个商品ID，然后分析这个商品N天的流量变化
    prodouct_keyword_data.dateRange = {'start_time':'2018-11-03','end_time':'2018-11-03'}
    # 过期
    # prodouct_keyword_data.cookies = {"_tb_token_":"e78516ab5d361","unb":"3397628220","tracknick":"%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97","tg":"0","v":"0","dnk":"%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97","existShop":"MTU0MTczNzUxMA%3D%3D","mt":"np=","uc3":"vt3=F8dByR%2FJztydMuKEnYc%3D&id2=UNN0nnQ5PzBj6g%3D%3D&nk2=sHmGuBnK6YrEuQ%3D%3D&lg2=VT5L2FSpMGV7TQ%3D%3D","t":"5b7cad4b69eb7fa1c14a798771984309","cna":"15ZiFBhnImkCAcFwhT7OdcJz","sg":"%E5%BA%9702","uc1":"cookie16=UtASsssmPlP%2Ff1IHDsDaPRu%2BPw%3D%3D&tmb=1&cookie21=U%2BGCWk%2F7oPGl&cookie15=WqG3DMC9VAQiUQ%3D%3D&existShop=true&pas=0&cookie14=UoTYN4Y2lYXbLw%3D%3D&tag=8&lng=zh_CN","skt":"d3177b7fa1e54bab","cookie1":"UNaOmjEdkzPF0d%2BKqRdPBjspDZcJ%2BrbnVjjQlzW63YA%3D","_cc_":"U%2BGCWk%2F7og%3D%3D","isg":"BBoatQCAFfBMF5lcWA99J8LoZ8k8S54lJcDk_iSTxq14l7rRDNvuNeBlY-0LXBa9","csg":"709e45ed","cookie2":"1b499555d5e3da52a795564ecbd8b17f","_l_g_":"Ug%3D%3D","JSESSIONID":"258509AA7163B232844E83EFD74CB31C","cookie17":"UNN0nnQ5PzBj6g%3D%3D","lgc":"%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97","_nk_":"%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97"}
    # 未过期
    prodouct_keyword_data.cookies = {
        "_tb_token_": "e8e8b51039488",
        "unb": "3397628220",
        "tracknick": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
        "tg": "0",
        "v": "0",
        "dnk": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
        "existShop": "MTU0MTc0NTcwNw%3D%3D",
        "mt": "np=",
        "uc3": "vt3=F8dByR%2FJzzsrh8Jzfjw%3D&id2=UNN0nnQ5PzBj6g%3D%3D&nk2=sHmGuBnK6YrEuQ%3D%3D&lg2=UtASsssmOIJ0bQ%3D%3D",
        "t": "987597c2a852b3e900a49233b6743b18",
        "cna": "15ZiFBhnImkCAcFwhT7OdcJz",
        "sg": "%E5%BA%9702",
        "uc1": "cookie16=URm48syIJ1yk0MX2J7mAAEhTuw%3D%3D&tmb=1&cookie21=VFC%2FuZ9aj38X&cookie15=UIHiLt3xD8xYTw%3D%3D&existShop=true&pas=0&cookie14=UoTYN4Yx9QSCdQ%3D%3D&tag=8&lng=zh_CN",
        "skt": "4ac934722d3b64e9",
        "cookie1": "UNaOmjEdkzPF0d%2BKqRdPBjspDZcJ%2BrbnVjjQlzW63YA%3D",
        "_cc_": "URm48syIZQ%3D%3D",
        "isg": "BGFhWBArntVDHzKhGBStmS9BfCR7DtUAyo2PI8M2XWjHKoH8C17l0I_oiNCs-W04",
        "csg": "6804891e",
        "cookie2": "1866dacadd5d0641b84ab5856c06d6b3",
        "_l_g_": "Ug%3D%3D",
        "JSESSIONID": "042ED5F914791D09D6FFAFE181EF7721",
        "cookie17": "UNN0nnQ5PzBj6g%3D%3D",
        "lgc": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97",
        "_nk_": "%5Cu5FA1%5Cu679C%5Cu65D7%5Cu8230%5Cu5E97"
    }   
    print (prodouct_keyword_data.Any_day_data(prodouct_keyword_data.dateRange))
    # print (prodouct_keyword_data.UnitPrice(prodouct_keyword_data.itemId,'2018-11-01'))

